#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "patients.h"
#include <stdlib.h> //pour l'appel de la fonction atoi()
#include <string.h> //pour l'appel de la fonction strcpy
#include <bilan.h>


void on_button1_clicked  (GtkWidget *objet_graphique, gpointer user_data)                  
{
GtkWidget * window3;

 window3 = create_window3();
    gtk_widget_show(window3);

}


void on_button10_clicked(GtkWidget *objet_graphique, gpointer user_data)                  
{GtkWidget * window4;

 window4 = create_window4();
    gtk_widget_show(window4);

}


void on_button12_clicked(GtkWidget *objet_graphique, gpointer user_data)                  
{
 
  GtkWidget * window6;
  GtkWidget * window7;
 struct patient pa[100];
  char chemin[20] = "patient.txt";
  int x;
  GtkWidget *entry38; 

	
        window6 = create_window6();
        gtk_widget_show(window6);

       
	entry38 = lookup_widget(objet_graphique,"entry38");


            //convertir la chaine saisie à partir de entry31 et la copier dans Pa.identifant
	int ident = atoi(gtk_entry_get_text(GTK_ENTRY(entry38)));
	
   
int k=0,j=0;

   recup_patient(pa,&k);
   while ((pa[j].iden!=ident)&&(j<k))
   {
       j++;
   }





       if(pa[j].iden==ident)
{

window7 = create_window7();
    gtk_widget_show(window7);

}
      else
  { window6 = create_window6();
    gtk_widget_show(window6);}

}
void
on_button11_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button13_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button84_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button85_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button86_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button87_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button88_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button89_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}




void
on_button15_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
	int x;/* la variables ds laquelle on a stocke le retour de la fct sauvegarder*/
        struct patient Pa;
	char chemin[20] = "patient.txt";
        int count=0;

	GtkWidget *entry19;
	GtkWidget *entry20;
	GtkWidget *entry21;
        GtkWidget *entry22;
	GtkWidget *entry23;
	GtkWidget *entry24;
        GtkWidget *entry25;
	GtkWidget *entry26;
	GtkWidget *entry27;
        GtkWidget *entry28;
	GtkWidget *entry29;
	GtkWidget *entry30;
	GtkWidget *entry71;

	GtkWidget *window4;
        GtkWidget *window5;
	
	entry19 = lookup_widget(objet_graphique, "entry19");
	entry20 = lookup_widget(objet_graphique, "entry20");
	entry21 = lookup_widget(objet_graphique, "entry21");
        entry22 = lookup_widget(objet_graphique, "entry22");
	entry23 = lookup_widget(objet_graphique, "entry23");
	entry24 = lookup_widget(objet_graphique, "entry24");
        entry25 = lookup_widget(objet_graphique, "entry25");
	entry26 = lookup_widget(objet_graphique, "entry26");   
        entry27 = lookup_widget(objet_graphique, "entry27");
	entry28 = lookup_widget(objet_graphique, "entry28");
	entry29 = lookup_widget(objet_graphique, "entry29");
        entry30 = lookup_widget(objet_graphique, "entry30");
	entry71 = lookup_widget(objet_graphique, "entry71");
/*int iden;
char nom[30];
char prenom[30];
char pays [30];
char profession [30];
char sexe [30];
char adresse [30];
int tel;
char photo[30];
char tsang[30];
int age ;
char nationnalite[30];*/
         
         //convertir la chaine saisie à partir de entry19 et la copier dans Pa.identifant
	Pa.iden = atoi(gtk_entry_get_text(GTK_ENTRY(entry19)));
	//copier la chaine saisie à partir de entry20 vers Pa.nom
	strcpy(Pa.nom, gtk_entry_get_text(GTK_ENTRY(entry20)));
	//copier la chaine saisie à partir de entry21 vers Pa.prenom
	strcpy(Pa.prenom, gtk_entry_get_text(GTK_ENTRY(entry21)));
	
        //copier la chaine saisie à partir de entry 22 vers Pa.pays
	strcpy(Pa.pays, gtk_entry_get_text(GTK_ENTRY(entry22)));
	//copier la chaine saisie à partir de entry23 vers Pa.profession
	strcpy(Pa.profession, gtk_entry_get_text(GTK_ENTRY(entry23)));
	
        //copier la chaine saisie à partir de entry24 vers Pa.sexe
	strcpy(Pa.sexe, gtk_entry_get_text(GTK_ENTRY(entry24)));
	//copier la chaine saisie à partir de entry25 vers Pa.adresse
	strcpy(Pa.adresse, gtk_entry_get_text(GTK_ENTRY(entry25)));
	//convertir la chaine saisie à partir de entry26 et la copier dans Pa.num de telephone
	Pa.tel=atoi(gtk_entry_get_text(GTK_ENTRY(entry26)));
        //copier la chaine saisie à partir de entry 27 vers Pa.photo link
	strcpy(Pa.photo,gtk_entry_get_text(GTK_ENTRY(entry27)));
	//copier la chaine saisie à partir de entry28 vers Pa.type du sang
	strcpy(Pa.tsang, gtk_entry_get_text(GTK_ENTRY(entry28)));
        //convertir la chaine saisie à partir de entry29 et la copier dans Pa.age
	Pa.age=atoi(gtk_entry_get_text(GTK_ENTRY(entry29)));
       //copier la chaine saisie à partir de entry 30 vers Pa.nationnalité
	strcpy(Pa.nationnalite,gtk_entry_get_text(GTK_ENTRY(entry30)));

        strcpy(Pa.observation,gtk_entry_get_text(GTK_ENTRY(entry71)));
        
    x=sauvegarder_patient(chemin,Pa);
//creation et affichage de la deuxième fenetre graphique
	
                window5=create_window5();
		gtk_widget_show(window5);

                      


}

void on_window2_destroy(GtkObject *object, gpointer user_data)
{
	gtk_main_quit();
}

void on_button16_clicked(GtkWidget *objet_graphique, gpointer user_data)
{

	struct patient Pa;
	char chemin[] = "personnes.txt";

        char Buf0[200] = "IDENTIFIANT: ";
	char Buf1[200] = "NOM: ";
	char Buf2[50] = "PRENOM: ";
        char Buf3[50] = "pays: ";
	char Buf4[50] = "profession: ";
	char Buf5[50] = "SEXE: ";
	char Buf6[50] = "ADRESSE: ";
        char Buf7[200] = "TEL: ";
        char Buf8[200] = "PHOTO: ";
	char Buf9[50] = "TSANG: ";
        char Buf10[50] = "AGE: ";
	char Buf11[50] = "NATIONNALITE: ";
	char Buf12[500]= "observations medicales: ";


	GtkWidget * label36;

	label36= lookup_widget(objet_graphique, "label36");

	lire_patient(chemin, &Pa);

	sprintf(Buf0, "  %s %d \n\n %s %s \n\n %s %s \n\n %s %s \n\n %s %s \n\n  %s %s \n\n  %s %s \n\n  %s %d \n\n  %s %s \n\n  %s %s \n\n  %s %d \n\n  %s %s \n\n", Buf0, Pa.iden, Buf1,
	        Pa.nom, Buf2, Pa.prenom,Buf3,Pa.pays,Buf4,Pa.profession,Buf5,Pa.sexe, Buf6, Pa.adresse,Buf7,Pa.tel,Buf8,Pa.photo,Buf9,Pa.tsang,Buf10,Pa.age,Buf11,Pa.nationnalite,Buf12,Pa.observation);

	gtk_label_set_text(GTK_LABEL(label36), Buf0);


}

void
on_button17_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button18_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button19_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button20_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button21_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button22_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button23_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}




void
on_button24_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget * window9;

 window9 = create_window9();
    gtk_widget_show(window9);
}


void
on_button34_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{GtkWidget * window11;

 window11 = create_window11();
    gtk_widget_show(window11);

}


void
on_button33_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}


void
on_button35_clicked       (GtkWidget *objet_graphique, gpointer user_data)   
{

	int x;/* la variables ds laquelle on a stocke le retour de la fct sauvegarder*/
        struct patient Pa;
	char chemin[20] = "personnels.txt";
        int count=0;

	GtkWidget *entry45;
	GtkWidget *entry46;
	GtkWidget *entry47;
        GtkWidget *entry48;
	GtkWidget *entry49;
	GtkWidget *entry50;
        GtkWidget *entry51;
	GtkWidget *entry52;
	GtkWidget *entry53;
        GtkWidget *entry54;
	GtkWidget *entry55;
	GtkWidget *entry56;


	GtkWidget *window11;
        //GtkWidget *window5;

	entry45 = lookup_widget(objet_graphique, "entry45");
	entry46 = lookup_widget(objet_graphique, "entry46");
	entry47 = lookup_widget(objet_graphique, "entry47");
        entry48 = lookup_widget(objet_graphique, "entry48");
	entry49 = lookup_widget(objet_graphique, "entry49");
	entry50 = lookup_widget(objet_graphique, "entry50");
        entry51 = lookup_widget(objet_graphique, "entry51");
	entry52 = lookup_widget(objet_graphique, "entry52");   
        entry53 = lookup_widget(objet_graphique, "entry53");
	entry54 = lookup_widget(objet_graphique, "entry54");
	entry55 = lookup_widget(objet_graphique, "entry55");
        entry56 = lookup_widget(objet_graphique, "entry56");

         
         //convertir la chaine saisie à partir de entry19 et la copier dans Pa.identifant
	Pa.iden = atoi(gtk_entry_get_text(GTK_ENTRY(entry45)));
	//copier la chaine saisie à partir de entry20 vers Pa.nom
	strcpy(Pa.nom, gtk_entry_get_text(GTK_ENTRY(entry46)));
	//copier la chaine saisie à partir de entry21 vers Pa.prenom
	strcpy(Pa.prenom, gtk_entry_get_text(GTK_ENTRY(entry47)));
	
        //copier la chaine saisie à partir de entry 22 vers Pa.pays
	strcpy(Pa.pays, gtk_entry_get_text(GTK_ENTRY(entry48)));
	//copier la chaine saisie à partir de entry23 vers Pa.profession
	strcpy(Pa.profession, gtk_entry_get_text(GTK_ENTRY(entry49)));
	
        //copier la chaine saisie à partir de entry24 vers Pa.sexe
	strcpy(Pa.sexe, gtk_entry_get_text(GTK_ENTRY(entry50)));
	//copier la chaine saisie à partir de entry25 vers Pa.adresse
	strcpy(Pa.adresse, gtk_entry_get_text(GTK_ENTRY(entry51)));
	//convertir la chaine saisie à partir de entry26 et la copier dans Pa.num de telephone
	Pa.tel=atoi(gtk_entry_get_text(GTK_ENTRY(entry52)));
        //copier la chaine saisie à partir de entry 27 vers Pa.photo link
	strcpy(Pa.photo,gtk_entry_get_text(GTK_ENTRY(entry53)));
	//copier la chaine saisie à partir de entry28 vers Pa.type du sang
	strcpy(Pa.tsang, gtk_entry_get_text(GTK_ENTRY(entry54)));
        //convertir la chaine saisie à partir de entry29 et la copier dans Pa.age
	Pa.age=atoi(gtk_entry_get_text(GTK_ENTRY(entry55)));
       //copier la chaine saisie à partir de entry 30 vers Pa.nationnalité
	strcpy(Pa.nationnalite,gtk_entry_get_text(GTK_ENTRY(entry56)));
        
    x=sauvegarder_patient(chemin,Pa);
//creation et affichage de la deuxième fenetre graphique
	
               
                      


}


void
on_button36_clicked      (GtkWidget *objet_graphique, gpointer user_data)

{char password[15];
char login[15];
char pswrd[15]="0000";
char log[15]="admin";

int x,y;

GtkWidget *entry68;
GtkWidget *entry69;


GtkWidget *window2;
entry68 = lookup_widget(objet_graphique, "entry68");
entry69 = lookup_widget(objet_graphique, "entry69");

strcpy(login, gtk_entry_get_text(GTK_ENTRY(entry68)));
strcpy(password, gtk_entry_get_text(GTK_ENTRY(entry69)));
x=strcmp(log,login);
y=strcmp(pswrd,password);

if ((x==0) && (y==0))
{window2 = create_window2();
gtk_widget_show(window2);


}
else
{ char ch[100];
//window2 = create_window2();
//gtk_widget_show(window2);
GtkWidget * label67;

	label67= lookup_widget(objet_graphique, "label67");
strcpy(ch,"password et login erroné");	
gtk_label_set_text(GTK_LABEL(label67),ch);
}

}


void
on_button38_clicked                  (GtkWidget *objet_graphique, gpointer user_data)                 
{	
	GtkWidget *window16;
	GtkWidget * label83;

        
char res[100];
struct event bilan[200];
int i=0,j=0;
char compare[40]="E01C01S01L01C02V01I01D01B01M01";
char vide[0]="";
analyser_fichier(bilan,&i);
for(j=0;j<i;j++)
{   if ((bilan[j].id / 10000) ==1 )
    {if((strcmp(bilan[j].passage,compare))!=0)
    	{

        strcpy(res,"il'y a des infraction/ consulter alerte.txt");
    	FILE *f;
    	f=fopen("alerte.txt","a");
    	if(f==NULL)
    	{
    	label83= lookup_widget(objet_graphique, "label83");
    	strcpy(res,"erreur");}
    	else {fprintf(f," le personnel avec l'id %d a un bilan erroné\n",bilan[j].id);}
    	fclose(f);

    	}
    }
    else if  ((bilan[j].id / 10000) ==2 )
    {	if((strcmp(bilan[j].passage,vide))!=0)
    	{

	label83= lookup_widget(objet_graphique, "label83");     
	strcpy(res,"il'y a des infraction/ consulter alerte.txt");
    	FILE *f;
    	f=fopen("alerte.txt","a");
    	if(f==NULL)
    	
    	{
	label83= lookup_widget(objet_graphique, "label83");    	
	strcpy(res,"erreur");}
    	else {fprintf(f," le patient avec l'id %d a un bilan erroné\n",bilan[j].id);}
    	fclose(f);
    	}
    }
    /*else {label83= lookup_widget(objet_graphique, "label83");  
	  strcpy(res,"Pas d'infractions dans le parcours");
	gtk_label_set_text(GTK_LABEL(label83),res);
    }*/
    
    
}//window16 = create_window16();
    //gtk_widget_show(window16);
	gtk_label_set_text(GTK_LABEL(label83),res);
//afficher(bilan);
}


void
on_button37_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget *window16;
window16 = create_window16();
    gtk_widget_show(window16);
}


void
on_button39_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget * window2;
    window2 = create_window2();
    gtk_widget_show(window2);

}


void
on_button40_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget * window3;
    window3 = create_window3();
    gtk_widget_show(window3);

}


void
on_button41_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget * window3;
    window3 = create_window3();
    gtk_widget_show(window3);
}


void
on_button42_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget * window3;
    window3 = create_window3();
    gtk_widget_show(window3);
}


void
on_button43_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{GtkWidget * window2;
    window2 = create_window2();
    gtk_widget_show(window2);


}


void
on_button44_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{
GtkWidget * window9;
    window9 = create_window9();
    gtk_widget_show(window9);
}


void
on_button45_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{   GtkWidget * window2;
    window2 = create_window2();
    gtk_widget_show(window2);


}


void
on_filechooserbutton1_file_set         (GtkFileChooserButton *filechooserbutton,
                                        gpointer         user_data)
{ /*GtkWidget * filechooserdialog1;
    filechooserdialog1 = create_filechooserdialog1();
    gtk_widget_show(filechooserdialog1);
*/

}


void
on_button48_clicked                    (GtkButton       *button,
                                        gpointer         user_data)
{

}

